
const form = layui.form;
const element = layui.element;
const laydate = layui.laydate
//==============表单================

// ===============tab栏切换=========================
layui.use('element', function () {
    //一些事件
    element.on('tab(demo)', function (data) {
        console.log(data);
    });
});
layui.use('laydate', function () {
    //执行一个laydate实例
    laydate.render({
        elem: '#creatTime' //指定元素
    });
})

// =====================初始化富文本编辑器===================
const E = window.wangEditor
const editor = new E('#contentBox')
editor.create()

$('#readCount').val(1)
// ===================所有分类的下拉框===================

axios.get('/ydd_article/list').then(res => {
    console.log(res)
    const newArr = res.data.data.data.map(item => {
        return `
    <option value="${item.cateid}">${item.catename}</option>
    `
    }).join('')
    // console.log(newArr)
    $('[name=cate_id]').append(newArr)
    form.render()
})

// ==========================添加文章==========================
// 数字增减
$('#jia').on('click', function () {
    let n = $(this).siblings('#readCount').val()
    n++
    $(this).siblings('#readCount').val(n)
})
$('#jian').on('click', function () {
    let n = $(this).siblings('#readCount').val()
    // console.log(n)
    if (n == 1) {
        return false
    }
    n--
    // console.log(n)
    $(this).siblings('#readCount').val(n)
})








// 给文件域注册change事件, 当选择的文件发生变化时触发
let saveImg = ''
let file = ''
$('#file').on('change', function (e) {
    e.preventDefault()
    file = this.files[0]
    let fd = new FormData()
    fd.append('file', file)
    axios({
        method: 'post',
        url: '/common/upload?type=images',
        data: fd,
    }).then(res => {
        // console.log(res.data.data.savePath)
        saveImg = res.data.data.savePath
        // $('img').attr('src', 'http://124.223.14.236:8060/' + saveImg)
    })
    // console.log('/upload/images/' + file.name)
    // console.log(file.name)
    let newSrc = URL.createObjectURL(file)
    // console.log(newSrc)
    // console.log('/upload/images/' + file.name);
    $('img').attr('src', newSrc)

    axios.defaults.headers["Content-Type"] = "multipart/form-data";
})
addRender()
function addRender() {
    $('#formAdd').on('submit', function (e) {
        e.preventDefault()
        let leibieid
        if ($('.layui-select-title input').val() === '时政') {
            leibieid = 3
        } else if ($('.layui-select-title input').val() === '图片') {
            leibieid = 4
        } else if ($('.layui-select-title input').val() === '娱乐') {
            leibieid = 5
        } else if ($('.layui-select-title input').val() === '汽车') {
            leibieid = 6
        } else if ($('.layui-select-title input').val() === '段子') {
            leibieid = 7
        } else if ($('.layui-select-title input').val() === '科技') {
            leibieid = 8
        } else if ($('.layui-select-title input').val() === '时尚') {
            leibieid = 9
        } else {
            leibieid = null
        }

        let data = {
            author: $('[name=author]').val(),
            cateid: leibieid,
            click: $('[name=click]').val(),
            content: editor.txt.html(),
            create_date: Date.parse($('#creatTime').val()),
            description: $('[name=description]').val(),
            ishot: $('[name=ishot]:checked').length,
            istop: $('[name=istop]:checked').length,
            keywords: $('[name=keywords]').val(),
            lovenum: $('[name=lovenum]').val(),
            // pic: saveImg,
            pic: '',
            status: '2',
            tags: '1',
            title: $('[name=title]').val(),
        }
        // console.log(data)
        // console.log(saveImg.split('/').join(''));
        let fd = new FormData(this)
        fd.append('ishot', 1)
        fd.append('istop', 1)
        fd.append('pic', '')
        fd.append('content', editor.txt.html())
        fd.append('description', $('[name=description]').val())
        fd.append('keywords', $('[name=keywords]').val())
        fd.append('status', 2)
        fd.append('tags', 'haha')
        // fd.forEach((k, v) => {
        //     console.log(k, v);
        // })
        axios.post('/ydd_article/add', fd).then(res => {
            // console.log(res)
            location.href = './article.html'
        })
    })
}






// ========================上传头像======================




// =========================返回列表=====================
$('.return').on('click', function () {
    location.href = './article.html'
})


